package testJdk.commonUse.string.regex;

import org.junit.Test;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 */
public class TestGroup {

    @Test
    public void test() {
        // 按指定模式在字符串查找
        String line = "This order was placed for QT3000! OK?";
        String regex = "(\\D*)(\\d+)(.*)";

        // 创建 Pattern 对象
        Pattern pattern = Pattern.compile(regex);

        // 创建 matcher 对象
        Matcher matcher = pattern.matcher(line);

        System.out.println("groupCount: " + matcher.groupCount());
        if (matcher.find()) {
            System.out.println("Found value: " + matcher.group(0));
            System.out.println("Found value: " + matcher.group(1));
            System.out.println("Found value: " + matcher.group(2));
            System.out.println("Found value: " + matcher.group(3));
        } else {
            System.out.println("NO MATCH");
        }
    }

    @Test
    public void test02() {
        String line = "DogdogDog";
        String regex = "(dog)";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(line);

        System.out.println("groupCount: " + matcher.groupCount());

        if (matcher.find()) {
            System.out.println(matcher.group(1));
            System.out.println("start: " +matcher.start());
            System.out.println("end: " +matcher.end());
        }
    }
}
